package com.martini.common.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.martini.common.domain.JwtVerifierInfo;

import java.util.Date;

/**
 * @author martini at 2021/10/12 9:23
 */
public class JwtUtils {
    /**
     * 生成json web token
     *
     * @return jwt
     */
    public static String generateJwt() {
        return JWT.create()
                .withIssuedAt(new Date())
                .withExpiresAt(new Date())
                .withSubject("1")
                .withAudience("id")
                .withIssuer("paper-composer")
                .withNotBefore(new Date())
                .withJWTId("jwt-id")
                .withClaim("username", "Jack")
                .sign(Algorithm.HMAC256("123456"));
    }

    public static JwtVerifierInfo verifyJwt(String token) {
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("123456"))
                .withIssuer("paper-composer")
                .build();
        DecodedJWT decodedJwt = jwtVerifier.verify(token);
        System.out.println(decodedJwt.getSubject());
        return new JwtVerifierInfo();
    }
}
